/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controlers;

import Pojos.SanPham;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author ToanNhan
 */
@WebServlet(name = "XemDanhSachSanPhamControler", urlPatterns = {"/XemDanhSachSanPhamControler"})
public class XemDanhSachSanPhamControler extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try {
            String command = request.getParameter("command");
            if (command == null) {
                response.sendRedirect("/WebsiteBanHang/XemDanhSachSanPham.jsp");
            } else {
                if (command.compareTo("layDanhSachSanPham") == 0) {
                    layDanhSachSanPham(request, response);
                }
                if (command.compareTo("laySanPhamTooltip") == 0) {
                    layDanhSachSanPhamTooltip(request, response);
                }
            }
        } finally {
        }
    }

    protected void layDanhSachSanPhamTooltip(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        PrintWriter out = response.getWriter();

        String maSanPham = request.getParameter("maSanPham");
        SessionFactory factory = new Configuration().configure("Pojos/hibernate.cfg.xml").buildSessionFactory();
        Session session = factory.openSession();
        try {
            Query query = session.createQuery("From SanPham as sp Where sp.maSanPham=?");
            query.setString(0, maSanPham);
            SanPham sp = (SanPham) query.uniqueResult();
            String body = "<table border=\"1px\"><tr><td width=\"200px\"><img src=\"/WebsiteBanHang" + sp.getHinhSanPham() + "\" width=\"150px\" height=\"150px\" /></td>";
            body += "<td><ul><li>Hang san xuat: " + sp.getHangSanXuat().getTenHangSanXuat() + "</li>" ;
            body += "<li>Xuat xu:" + sp.getXuatXu() + "</li>";
            body += "<li>Số lược xem:" + sp.getSoLuotXem() + "</li>";
            body += "<li>Số lược mua:" + sp.getSoLuotMua() + "</li>";
            body += "</ul></td></tr></table>";
            out.print(body);
            out.flush();
            out.close();
            session.close();
            factory.close();
        }finally{
            session.close();
            factory.close();
        }
    }

    protected void layDanhSachSanPham(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String maDanhMuc = request.getParameter("maDanhMuc");
        if (maDanhMuc == null) {
            maDanhMuc = "DM01";
        }
        int trangHienTai = 1;
        if (request.getParameter("trangHienTai") != null) {
            trangHienTai = Integer.valueOf(request.getParameter("trangHienTai"));
        }
        int soLuongTrenTrang = 20;
        if (request.getParameter("soLuongTrenTrang") != null) {
            soLuongTrenTrang = Integer.valueOf(request.getParameter("soLuongTrenTrang"));
        }
        SessionFactory factory = new Configuration().configure("Pojos/hibernate.cfg.xml").buildSessionFactory();
        Session session = factory.openSession();
        Query query = session.createQuery("From SanPham Where danhMucSanPham.maDanhMuc = ?");
        query.setString(0, maDanhMuc);

        List<SanPham> lstSanPham = (List<SanPham>) query.list();
        int tongSoLuong = lstSanPham.size();
        if (tongSoLuong > 0) {
            int beginIndex = (trangHienTai - 1) * soLuongTrenTrang;
            int endIndex = beginIndex + soLuongTrenTrang;
            if (endIndex < tongSoLuong) {
                lstSanPham = lstSanPham.subList(beginIndex, endIndex);
            } else {
                lstSanPham = lstSanPham.subList(beginIndex, tongSoLuong - 1);
            }
            int tongSoTrang = tongSoLuong / soLuongTrenTrang;
            if (tongSoLuong % soLuongTrenTrang != 0) {
                tongSoTrang++;
            }

            request.setAttribute("tongSoTrang", tongSoTrang);
        }
        request.setAttribute("danhSachSanPham", lstSanPham);
        session.close();
        factory.close();
        request.getRequestDispatcher("XemDanhSachSanPham.jsp").forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
